package cn.xm.org.controller;

import cn.xm.basic.result.AjaxResult;
import cn.xm.org.domain.Department;
import cn.xm.org.query.DepartmentQuery;
import cn.xm.basic.result.PageList;
import cn.xm.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门管理")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    @GetMapping("/deptTree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "凭id查询")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me();
    }

    @PutMapping("/save")
    @ApiOperation(value = "新增或修改")
    public AjaxResult addOrUpdate(@RequestBody @Valid Department department) {
        if (department.getId() == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me();
    }

    @PostMapping("/query")
    @ApiOperation(value = "分页查询")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryByPage(departmentQuery);

    }

    @PatchMapping("/batchdel")
    public AjaxResult batchdelete(@RequestBody List<Long> ids) {
        departmentService.batchDelete(ids);
        return new AjaxResult().me();
    }

    @GetMapping("/get_dept_tree")
    public List<Department> getDeptTree() {
        return departmentService.getDeptTree();
    }


}
